import { getArticleLabelList, get_brochure_knowledge_list, get_brochure_knowledge_right_list, get_detail_list, get_shouping_list } from '@/services';
import { IArticleLabelList, IBrochure_knowledge_list, IBrochure_knowledge_right_toplist, Idetail_list, Ishouping_list } from '@/utils/brochure/beochureType';
import { Effect, Reducer } from 'umi';

// 定义模块内部状态的类型
export interface IBrochureArticleModelState {

  // 小册左侧列表
  brochure_article_list: IBrochure_knowledge_list[];
  // 小册右侧数据
  brochure_article_right_top_list: IBrochure_knowledge_right_toplist[];
  // 小册详情数据
  // brochure_detail_list: Idetail_list;
  brochure_detail_list: Partial<Idetail_list>;
  // 首屏列表数据
  shouping_list: Partial<Ishouping_list[]>
  // 文章分类数据
  articleLabelList: Partial<IArticleLabelList[]>
}

// 定义模块的类型
export interface brochureModelType {
  namespace: 'brochureEffect';
  state: IBrochureArticleModelState;
  effects: {
    get_brochure_knowledge_list: Effect;
    get_brochure_knowledge_right_list: Effect;
    get_detail_list: Effect;
    get_shouping_list: Effect;
    getArticleLabelList: Effect;
  };
  reducers: {
    save: Reducer<IBrochureArticleModelState>;
  };
}

const BrochureModel: brochureModelType = {
  // 模块命名空间
  namespace: 'brochureEffect',

  // 模块内部状态
  state: {
    brochure_article_list: [],
    brochure_article_right_top_list: [],
    brochure_detail_list: {},
    shouping_list: [],
    articleLabelList:[]
  },

  // 副作用
  effects: {
    // 小册左侧列表数据
    *get_brochure_knowledge_list({ payload }, { call, put }) {
      const result = yield call(get_brochure_knowledge_list);
      if (result.statusCode === 200) {
        yield put({
          type: "save",
          payload: {
            brochure_article_list: result.data[0]
          }
        })
      }
    },
    // 小册左侧列表数据
    *get_brochure_knowledge_right_list({ payload }, { call, put }) {
      const result = yield call(get_brochure_knowledge_right_list);
      if (result.statusCode === 200) {
        yield put({
          type: "save",
          payload: {
            brochure_article_right_top_list: result.data
          }
        })
      }
    },
    // 小册详情数据
    *get_detail_list({ payload }, { call, put }) {

      const result = yield call(get_detail_list, payload);

      if (result.statusCode === 200) {
        yield put({
          type: "save",
          payload: {
            brochure_detail_list: result.data
          }
        })
      }
    },
    // 首屏微信小程序数据
    *get_shouping_list({ payload }, { call, put }) {

      const result = yield call(get_shouping_list);

      if (result.statusCode === 200) {
        yield put({
          type: "save",
          payload: {
            brochure_detail_list: result.data[0]
          }
        })
      }
    },
    // 文章分类数据
    *getArticleLabelList({ payload }, { call, put }) {

      const result = yield call(getArticleLabelList);

      if (result.statusCode === 200) {
        yield put({
          type: "save",
          payload: {
            articleLabelList: result.data
          }
        })
      }
    },
  },

  // 同步修改
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    }
  }
};

export default BrochureModel;